package com.xnyzc.lhy.didi.controller.other;

import com.xnyzc.lhy.common.component.didi.DiDiUrlConstant;
import com.xnyzc.lhy.common.entity.DiDiRv;
import com.xnyzc.lhy.common.exception.DiDiException;
import com.xnyzc.lhy.common.penum.EDiDiErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.retry.RetryUtil;
import com.xnyzc.lhy.didi.entity.param.other.BindCouponParam;
import com.xnyzc.lhy.didi.service.other.CouponService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 优惠券相关接口
 *
 * @author zhengxu
 * @date 2019/12/11
 */
@RestController
@RequestMapping("oa/didi/coupon")
public class CouponController {
    @Autowired
    private CouponService couponService;

    @ApiOperation(value = "绑定优惠券", notes = "绑定优惠券")
    @PostMapping(value = "/bindByUidBack")
    public DiDiRv bindByUidBack(@ApiParam(name = "couponParam", value = "绑定优惠券信息")
                                @RequestBody BindCouponParam couponParam) {
        return couponService.bindByUidBack(couponParam);
    }

    /**
     * 批量关闭工单
     */
    @GetMapping("RecordGenurater")
    public void recordGenurater(@RequestParam("amountInUrl") String amountInUrl, @RequestParam("amountOutUrl") String amountOutUrl) {

        Integer count = 0;

        try {
            BufferedReader amount = new BufferedReader(new InputStreamReader(new FileInputStream(new File(amountInUrl)), "UTF-8"));
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(amountOutUrl)), "UTF-8"));
            String lineTxt;
            while ((lineTxt = amount.readLine()) != null) {
                if (lineTxt.contains("GD")) {
                    lineTxt = lineTxt.substring(lineTxt.indexOf("G"), lineTxt.indexOf("|", 10));
                    System.out.println(lineTxt);
                    count++;

                    // 访问线上关闭工单接口
                    String url = "http://api.chuangwetech.com/didi/v1/oa/didi/work/getUpdateWork";
                    Map<String, Object> map = new HashMap<>();
                    map.put("worksheet_id", lineTxt);
                    map.put("action", "2");
                    map.put("content", "客服关闭");
                    map.put("dict_id", "D0002");
                    JSONObject json = JSONObject.fromObject(map);
                    String data = null;
                    try {
                        data = RetryUtil.requestMethodJson(json, url, 5, 1000L, 1.3);
                    } catch (Exception e) {
                        throw DiDiException.create(EDiDiErrorCode.httpRequestDidiUrlError);
                    }
                    if (CheckUtil.strIsEmpty(data)) {
                        System.out.println("关单失败工单：" + lineTxt);
                        writer.write(lineTxt + "\n");
                    }
                    com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(data);
                    if (jsonObject.get(DiDiUrlConstant.ERRNO).equals(EDiDiErrorCode.success.getValue())) {
                    } else {
                        System.out.println("关单失败工单：" + lineTxt);
                        writer.write(lineTxt + "\n");
                    }
                }
            }

            System.out.println("共计：[" + count + "]条");

            amount.close();
            writer.close();
        } catch (Exception e) {
            System.err.println("read errors :" + e);
        }

    }
}
